package com.class5.springboot.htmlcontroller;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.TbXtRole;
import com.class5.springboot.frame.web.SuperRestController;
import com.class5.springboot.frame.web.result.ResultData;
import com.class5.springboot.service.facade.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class HRoleController extends SuperRestController {

    @Autowired
    private IRoleService roleService;


    @RequestMapping("/html/findRoleList")
    @ResponseBody
    public ResultData findRoleList(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        log.info("获取角色列表");
        List roleList = roleService.findRoleList(param);
        return successWraper(roleList);
    }

    @RequestMapping("html/getMenulistByRoleId")
    @ResponseBody
    public ResultData getMenulistByRoleId(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        Long roleId = Long.parseLong(req.getParameter("roleId"));
        log.info("查询用户信息id:{}",roleId);
        List menuList = roleService.getMenulistByRoleId(roleId);
        return successWraper(menuList);
    }

    @RequestMapping("/html/createRole")
    @ResponseBody
    public ResultData createRole(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        String rolename = req.getParameter("rolename");
        param.put("roleName",rolename);
        log.info("添加角色信息rolename:{}",rolename);
        roleService.createRole(param);
        return successWraper("新增成功");
    }

    @RequestMapping("/html/findRoleById")
    @ResponseBody
    public ResultData findRoleById(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        Long id = Long.parseLong(req.getParameter("id"));
        param.put("id",id);
        log.info("查询角色信息id:{}",id);
        TbXtRole role = roleService.findRoleById(id);
        return successWraper(role);
    }

    @RequestMapping("/html/updateRole")
    @ResponseBody
    public ResultData updateRole(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        Long id = Long.parseLong(req.getParameter("id"));
        String roleName = req.getParameter("rolename");
        param.put("id",id);
        param.put("roleName",roleName);
        log.info("修改角色信息id:{}，rolename:{}",id,roleName);
        roleService.updateRole(param);
        return successWraper("修改成功");
    }

    @RequestMapping("/html/deleteRoleById")
    @ResponseBody
    public ResultData deleteRoleById(HttpServletRequest req) {
        JSONObject param = new JSONObject();
        Long id = Long.parseLong(req.getParameter("id"));
        log.info("删除角色信息id:{}",id);
        roleService.deleteRoleById(id);
        return successWraper("删除成功");
    }
}
